package com.demo.support.util;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import sun.misc.BASE64Decoder;
@SuppressWarnings("restriction")
public class RSAUtil {

	private final static String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFirtGon0Rm3OdRmNG/LtxU0R8qIfD28Y+hV6ywlXxFK+nUGzy27iVagcZawfuK/2ZMFJ9UXJZwcF/zGYPiypetlF6DsMqaw2mfC3vCrWkHSZ2ABDVaclVwkvbzrceK7Kk0ZUhIxKHeIV0roQHyACdGoax/KeLW+05PF38HB4CGQIDAQAB";
	private final static String privateKeyStr = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMWKu0aifRGbc51GY0b8u3FTRHyoh8Pbxj6FXrLCVfEUr6dQbPLbuJVqBxlrB+4r/ZkwUn1RclnBwX/MZg+LKl62UXoOwyprDaZ8Le8KtaQdJnYAENVpyVXCS9vOtx4rsqTRlSEjEod4hXSuhAfIAJ0ahrH8p4tb7Tk8XfwcHgIZAgMBAAECgYEAsB0rMaIT1uq8fG8Qe20+v5345RprT9ZUO9EzyDMx46kJoSU2vJ3fRnYstiCSBWj6wuQt5sQCKrtvA7z7EMiFkDCyvNh3zjrOKe8hlPQ5C6fGoGUUQ4jAGXyViXdANUkQvfZwvoWo+fqqevEQR/NjPuMK6I2uvE2rpxBB1yF4c8ECQQDz/+bdV/zZwzKemgykTutAF0W3Qdpuw8p+3ti39W2vZAw8DirWhl0dY6U6vo1nFDrm8oU3h7AOtsPp+gZhE94lAkEAz0HmbT/BMZ2TDk04RMui72GhInWGmsD5HMSh3p0uQ1JVUs/dir+6YosiNhV2KAU+KIeX1TLhif4XipwCnXmv5QJAeTdSS/ggR1av2RcS3LXQLUDig44VpB1khe7Tn2AwI4gCg/tN0NFjhOwx86MiwGjOclldUR75mfW2EpEBhkzGeQJBAKu0hgmmkxDe4hEsKlzcdF1r/coFogGdtASAZzZ98/D0Shn0ei9UDj9L04kJGL2pMkjzpPeNUss3Bhvgik4anX0CQHryYSJXMQ0bb4hOPKEnU+do3XQHpnIx921gQCDhOiJUsD8fkVQj7pFAk3gTWcD/FsnzuYmxaxNHxXf18E9mJpA=";
	
	public static PrivateKey getPrivateKey() {
		
		PrivateKey privateKey = null;
		try{
			byte[] keyBytes;  
	        keyBytes = (new BASE64Decoder()).decodeBuffer(privateKeyStr);  

	        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);  
	        KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
	        privateKey = keyFactory.generatePrivate(keySpec);  
		}catch(Exception e) {
			e.printStackTrace();
		}
		
        return privateKey;  
	}
	
	public static PublicKey getPublicKey() {
		
		 PublicKey publicKey = null;
		try{
			byte[] keyBytes;  
	        keyBytes = (new BASE64Decoder()).decodeBuffer(publicKeyStr);  

	        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
	        KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
	        publicKey = keyFactory.generatePublic(keySpec);	
		}catch(Exception e) {
			e.printStackTrace();
		}
		  
        return publicKey;  
	}
	public static PublicKey getPublicKey(final String  publicKeyStr) {
		
		 PublicKey publicKey = null;
		try{
			byte[] keyBytes;  
	        keyBytes = (new BASE64Decoder()).decodeBuffer(publicKeyStr);  

	        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
	        KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
	        publicKey = keyFactory.generatePublic(keySpec);	
		}catch(Exception e) {
			e.printStackTrace();
		}
		  
       return publicKey;  
	}
}
